# -*- coding: utf-8 -*-
from djwatch.visualization import GraphData

FILESYSTEM_TITLE = u'%(mount_point)s [%(device)s]'
K_FREE_SPACE = 1.1

def filesystem(stat):
    titles = set()
    for t, data in stat:  #@UnusedVariable
        plugin_data = data.get('filesystem', {})
        for k, data in plugin_data.items():
            data['mount_point'] = k
            titles.add((FILESYSTEM_TITLE % data, k))

    titles = sorted(titles)
    graph_data = GraphData(u'Свободное место на диске, Гб', [title for title, k in titles])
    for t, data in stat:
        plugin_data = data.get('filesystem', {})
        for i, (title, k) in enumerate(titles): #@UnusedVariable
            d = plugin_data.get(k, {})
            free = toGB(d.get('total', 0) - d.get('used', 0))
            graph_data.rows[i].append((t, free))
            graph_data.update_max(free * K_FREE_SPACE)
    return graph_data

def diskusage(stat):
    titles = set()
    for t, data in stat:  #@UnusedVariable
        titles.update(data.get('diskusage', {}))
    titles = sorted(titles)

    graph_data = GraphData(u'Наблюдение за файлами и каталогами, Мб', titles)
    for t, data in stat:
        plugin_data = data.get('diskusage', {})
        for i, title in enumerate(titles): #@UnusedVariable
            size = toMB(plugin_data.get(title, 0))
            graph_data.rows[i].append((t, size))
            graph_data.update_max(size * K_FREE_SPACE)
    return graph_data

def cpu(stat):
    graph_data = GraphData('CPU', [
        u'1 минута',
        u'5 минут',
        u'10 минут',
    ])
    for t, data in stat:
        plugin_data = data.get('cpu', {})
        if plugin_data:
            graph_data.rows[0].append((t, plugin_data['1min_usage']))
            graph_data.rows[1].append((t, plugin_data['5min_usage']))
            graph_data.rows[2].append((t, plugin_data['10min_usage']))
            graph_data.update_max(plugin_data['1min_usage'] * K_FREE_SPACE)
            graph_data.update_max(plugin_data['5min_usage'] * K_FREE_SPACE)
            graph_data.update_max(plugin_data['10min_usage'] * K_FREE_SPACE)
    return graph_data

def toGB(bytes):
    return float(bytes) / 1024 ** 3

def toMB(bytes):
    return float(bytes) / 1024 ** 2

def memory(stat):
    graph_data = GraphData(u'Память, Гб', [
        u'Своп',
        u'Занятая память',
        u'Активно используется',
#        u'Inactive',
    ])
    for t, data in stat:
        plugin_data = data.get('memory', {})
        if plugin_data:
            mem_total = plugin_data.get('mem_total', 0)
            mem_free = plugin_data.get('mem_free', 0)
            swap_total = plugin_data.get('swap_total', 0)
            swap_free = plugin_data.get('swap_free', 0)
            graph_data.rows[0].append((t, toGB(swap_total - swap_free)))
            graph_data.rows[1].append((t, toGB(mem_total - mem_free)))
            graph_data.rows[2].append((t, toGB(plugin_data.get('active', 0))))
#            graph_data.rows[3].append((t, toGB(plugin_data.get('inactive', 0))))
            graph_data.update_max(toGB(mem_total))
#            graph_data.update_max(toGB(plugin_data['swap']['total']))
    return graph_data

def processes(stat, min_number=5):
    names = set()
    for t, data in stat: #@UnusedVariable
        names.update(data.get('processes', {}))
    names = sorted(names)

    title2index = dict((t, i) for i, t in enumerate(names))
    max_number = min_number
    rows = [[] for _ in names]
    max_n_process = dict((name, 0) for name in names)
    for t, data in stat:
        processes = data.get('processes', {})
        for name, number in processes.items():
            rows[title2index[name]].append((t, number))
            max_n_process[name] = max(max_n_process[name], number)
            max_number = max(max_number, number * K_FREE_SPACE)

    names_, rows_ = [], []
    for name, row in sorted(zip(names, rows), key=lambda (name, row): name):
        if max_n_process[name] >= min_number:
            names_.append(name)
            rows_.append(row)

    title = u'Наиболее многочисленные процессы (> %d), шт.' % min_number
    graph_data = GraphData(title, names_)
#    print graph_data.rows
    graph_data.rows = rows_
    graph_data.min = min_number
    graph_data.max = max_number
    return graph_data
